package com.mshop.util;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class GetHttpServletRequestWrapper extends HttpServletRequestWrapper  {
	private String charset = "UTF-8";   

	public GetHttpServletRequestWrapper(HttpServletRequest request, String charset) {
		super(request);
		 this.charset = charset;   
	}

	@Override
	public String getParameter(String name) {
		String value = super.getParameter(name);
		value = value == null ? null : convert(value);
		return value;
	}

	@Override
	public String[] getParameterValues(String name) {
		String[] values=super.getParameterValues(name);
		if(values==null){
			return null;
		}
		for(int i=0;i<values.length;i++){
			values[i]=values[i] == null ? null : convert(values[i]);
		}
		return values;
	}

	public String convert(String target) {
		try {
			return new String(target.trim().getBytes("ISO-8859-1"), charset);
		} catch (UnsupportedEncodingException e) {
			return target;
		}
	}

}
